using System.Collections.Generic;
using UnityEngine;

public class MessageManager : MonoBehaviour
{
	public static MessageManager instance;

	public bool showTestMessage;

	public ColorForMessage[] sampleColor;

	public List<ExampleMessage> listExampleMsg = new List<ExampleMessage>();

	public List<MessageBase> messages = new List<MessageBase>();

	public List<MessageBase> listPlayMessages = new List<MessageBase>();

	private void Awake()
	{
		instance = this;
	}

	private void Update()
	{
		if (messages.Count > 0 && listPlayMessages.Count == 0)
		{
			MessageBase messageBase = messages[0];
			listPlayMessages.Add(messageBase);
			messageBase.StartMessage();
		}
		if (showTestMessage)
		{
			showTestMessage = false;
			AddMessage();
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public static void OnCompleteMessage(MessageBase comlMsg)
	{
		if (instance != null)
		{
			instance.listPlayMessages.Remove(comlMsg);
			instance.messages.Remove(comlMsg);
			Object.Destroy(comlMsg.gameObject, 0.05f);
		}
	}

	public void CreateMessage(string text, TypeAnimMessage type = TypeAnimMessage.bottomToUp)
	{
		CreateMessage(text, Color.white, Color.gray, type, 2f);
	}

	public void CreateMessage(string text, IdClForMsg idColor, TypeAnimMessage type = TypeAnimMessage.bottomToUp, float needTime = 2f)
	{
		ColorForMessage colorForMessage = instance.GetSampleColor(idColor);
		if (colorForMessage != null)
		{
			CreateMessage(text, colorForMessage.clFrg, colorForMessage.clBkg, type, needTime);
		}
	}

	public void CreateMessage(string text, Color clF, Color clB, TypeAnimMessage type = TypeAnimMessage.bottomToUp, float time = 2f)
	{
		ExampleMessage exampleMsg = GetExampleMsg(type);
		GameObject gameObject = null;
		MessageBase messageBase = null;
		if (exampleMsg != null)
		{
			gameObject = Object.Instantiate(exampleMsg.exampleMsg.gameObject);
			gameObject.transform.parent = base.transform;
			gameObject.transform.position = Vector3.zero;
			gameObject.transform.localScale = Vector3.one;
			messageBase = gameObject.GetComponent<MessageBase>();
			switch (type)
			{
			case TypeAnimMessage.bottomToUp:
				messageBase.SetColor(clF, clB);
				messageBase.SetText(text);
				break;
			case TypeAnimMessage.normalMsg:
			case TypeAnimMessage.missionComplete:
				messageBase.SetColor(clF, clB);
				messageBase.SetText(text);
				break;
			case TypeAnimMessage.longTimeMessage:
				messageBase.SetColor(clF, clB);
				messageBase.SetText(text);
				messageBase.SetTime(time);
				break;
			case TypeAnimMessage.levelUp:
				if ((bool)messageBase)
				{
					messageBase.SetText(text);
				}
				break;
			}
		}
		if (messageBase != null)
		{
			messages.Add(messageBase);
			messageBase.Wait();
		}
	}

	public void AddMessage()
	{
	}

	public ColorForMessage GetSampleColor(IdClForMsg needId)
	{
		for (int i = 0; i < sampleColor.Length; i++)
		{
			if (sampleColor[i].idColor == needId)
			{
				return sampleColor[i];
			}
		}
		return null;
	}

	public ExampleMessage GetExampleMsg(TypeAnimMessage needId)
	{
		for (int i = 0; i < listExampleMsg.Count; i++)
		{
			if (listExampleMsg[i].idMsg == needId)
			{
				return listExampleMsg[i];
			}
		}
		return null;
	}

	public void SetActivate(bool val)
	{
		for (int i = 0; i < listPlayMessages.Count; i++)
		{
			listPlayMessages[i].gameObject.SetActive(val);
		}
	}
}
